﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Data;

namespace Slja.GSMUtility

{
    public class SmsUti
    {
        public static List<Slja.GSMUtility.Model.sms_MsgRecive> AssignReciveModelList(List<SmsMsgInfo> msgList)
        {
            // List<SmsMsgInfo> msgList = ConvertRecivedMsgFormat(arrUnRead);

            List<Slja.GSMUtility.Model.sms_MsgRecive> listModel = new List<Slja.GSMUtility.Model.sms_MsgRecive>();
            //写入数据库
            Slja.GSMUtility.Model.sms_MsgRecive model = null;
            for (int i = 0; i < msgList.Count; i++)
            {
                model = new Slja.GSMUtility.Model.sms_MsgRecive();
                model.Code = msgList[i].Code;
                model.MsgContent = msgList[i].MsgBody;
                model.MsgIndex = msgList[i].Index;
                model.ReadMemo = "";
                model.RecvTime = msgList[i].ReciveTime;
                model.SendNO = msgList[i].SendNO;
                model.Status = 0;//未读(放入数据库，尚未读)
                listModel.Add(model);
            }
            return listModel;
        }
        public static List<SmsMsgInfo> ConvertRecivedMsgFormat(string[] arrMsgStr)
        {
            List<SmsMsgInfo> list = new List<SmsMsgInfo>();
            string[] arrInfo;
            for (int i = 0; i < arrMsgStr.Length; i++)
            {
                if (!string.IsNullOrEmpty(arrMsgStr[i]))
                {
                    char Separator = (char)1;
                    arrInfo = arrMsgStr[i].Split(Separator);//信息字符串（[MMNN,中心号码，手机号码，发送时间，短信内容,短信序号] MM这批短信总条数 NN本条所在序号）
                    SmsMsgInfo info = new SmsMsgInfo();
                    info.Code = "";
                    info.Index = int.Parse(arrInfo[5]);
                    info.MsgBody = arrInfo[4];
                    info.ReciveTime = DateTime.ParseExact(arrInfo[3], "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
                    info.SendNO = arrInfo[2];
                    list.Add(info);
                }
            }
            return list;
        }
        public static string GetXMLFromMsgIndexInfo(string UserID)
        {
            DAL.sms_MsgSend ms = new DAL.sms_MsgSend();
            DataTable dt = ms.GetDataFromMsgIndexInfo(UserID);
            StringBuilder sb = new StringBuilder();
            sb.Append("<xml>");
            DataRow dr = null;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sb.Append("<row>");
                dr = dt.Rows[i];
                sb.AppendFormat("<Reciver>{0}</Reciver>",dr["Reciver"].ToString());
                sb.AppendFormat("<RecvNO>{0}</RecvNO>", dr["RecvNO"].ToString());
                sb.AppendFormat("<MsgCount>{0}</MsgCount>", dr["MsgCount"].ToString());
                sb.AppendFormat("<MsgContent>{0}</MsgContent>", dr["MsgContent"].ToString());
                string strDtm;
                if (((DateTime)dr["SendTime"]).ToString("yyyy-MM-dd") == DateTime.Today.ToString("yyyy-MM-dd"))
                {
                    strDtm = ((DateTime)dr["SendTime"]).ToLongTimeString();
                }
                else
                {
                    strDtm = ((DateTime)dr["SendTime"]).ToShortDateString();
                }
                sb.AppendFormat("<SendTime>{0}</SendTime>", strDtm);
                sb.Append("</row>");
            }
            sb.Append("</xml>");
            return sb.ToString();
        }
        public static string GetXMLFromLinkmanMobileNO(string UserID, string MobileNO)
        {
            DAL.sms_MsgSend ms = new DAL.sms_MsgSend();
            DataTable dt = ms.GetDataFromLinkmanMobileNO(UserID, MobileNO);
            StringBuilder sb = new StringBuilder();
            sb.Append("<xml>");
            DataRow dr = null;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sb.Append("<row>");
                dr = dt.Rows[i];
                int nMsgType = (int)dr["MsgType"];
                byte nStatus = (byte)dr["Status"];
                string strStatus = "";
                if (nMsgType == 1 && nStatus== 3)//发送，失败
                {
                    strStatus = "(失败!)";
                }
                else
                {
                    strStatus = "";
                }

                string MsgType1 = nMsgType == 1 ? "→" : "";
                string MsgType2 = nMsgType == 2 ? "←" : "";
                sb.AppendFormat("<MsgType1>{0}</MsgType1>", MsgType1);
                sb.AppendFormat("<MsgType2>{0}</MsgType2>", MsgType2);
                sb.AppendFormat("<Status>{0}</Status>", strStatus);
                //sb.AppendFormat("<Dtm>{0}</Dtm>", dr["Dtm"].ToString());
                sb.AppendFormat("<MsgContent>{0}</MsgContent>", dr["MsgContent"].ToString());
                string strDtm;
                if (((DateTime)dr["Dtm"]).ToString("yyyy-MM-dd") == DateTime.Today.ToString("yyyy-MM-dd"))
                {
                    strDtm = ((DateTime)dr["Dtm"]).ToLongTimeString();
                }
                else
                {
                    strDtm = ((DateTime)dr["Dtm"]).ToShortDateString();
                }
                sb.AppendFormat("<Dtm>{0}</Dtm>", strDtm);
                sb.Append("</row>");
            }
            sb.Append("</xml>");
            return sb.ToString();
        }
    }
    /// <summary>
    /// 短信读取状态
    /// </summary>
    public enum SmsMsgType
    {
        WaitingRead=0,
        HasRead=1,
        WaitingSend=2,
        HasSent=3,
        All=4
        //0：未读短信；1：已读短信；2：待发短信；3：已发短信；4：全部短信
    }
    public struct SmsMsgInfo
    {
        public int Index;
        public string Code;
        public string SendNO;
        public string MsgBody;
        public DateTime ReciveTime;

    }
    public struct MsgStatus
    {
        public MsgStatus(int id, byte status, string recvno)
        {
            MsgID = id;
            Status = status;
            RecvNO = recvno;
            
        }
        public int MsgID;
        public byte Status;
        public string RecvNO;
    }
}